package com.xuecheng.content.api;

import com.xuecheng.base.exception.ValidationGroups;
import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.content.model.dto.AddCourseDto;
import com.xuecheng.content.model.dto.EditCourseDto;
import com.xuecheng.content.model.dto.QueryCourseParamsDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.vo.CourseBaseInfoVo;
import com.xuecheng.content.service.CourseBaseInfoService;
import com.xuecheng.content.util.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/course")
@Api(tags = "课程信息管理接口")
public class CourseBaseInfoController {

    @Autowired
    private CourseBaseInfoService courseBaseInfoService;

    @PostMapping("/list")
    //@PreAuthorize("hasAuthority('xc_teachmanager_course_list')") // 指定权限标识符，拥有此权限才可以访问此方法
    @ApiOperation(value = "课程查询接口")
    public PageResult<CourseBase> list(PageParams pageParams,@RequestBody QueryCourseParamsDto queryCourseParamsDto) {

        // 当前登录用户
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        // 用户所属的机构id
        Long companyId = null;
        if(StringUtils.isNotEmpty(user.getCompanyId())) {
            companyId = Long.parseLong(user.getCompanyId());
        }

        PageResult<CourseBase> courseBasePageResult = courseBaseInfoService.queryCourseBaseList(companyId, pageParams, queryCourseParamsDto);
        return courseBasePageResult;
    }

    @PostMapping
    @ApiOperation(value = "新增课程")
    public CourseBaseInfoVo createCourseBase(@RequestBody @Validated(ValidationGroups.Insert.class) AddCourseDto addCourseDto) {

        // 获取到用户所属机构的id
        Long companyId = 1232141425L;

        CourseBaseInfoVo courseBase = courseBaseInfoService.createCourseBase(companyId, addCourseDto);

        return courseBase;
    }

    @GetMapping("/{courseId}")
    @ApiOperation(value = "根据课程id查询接口")
    public CourseBaseInfoVo getCourseBaseInfo(@PathVariable Long courseId) {
        //取出当前用户身份
        // Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        // System.out.println(principal);
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        System.out.println(user.getUsername());
        CourseBaseInfoVo courseBaseInfo = courseBaseInfoService.getCourseBaseInfo(courseId);
        return courseBaseInfo;
    }

    @PutMapping
    @ApiOperation(value = "修改课程")
    public CourseBaseInfoVo modifyCourseBase(@RequestBody @Validated(ValidationGroups.Update.class) EditCourseDto editCourseDto) {

        // 获取到用户所属机构的id
        Long companyId = 1232141425L;
        CourseBaseInfoVo courseBaseInfoVo = courseBaseInfoService.updateCourseBase(companyId, editCourseDto);
        return courseBaseInfoVo;
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除课程")
    public void deleteCourseBase(@PathVariable Long id) {
        // 获取到用户所属机构的id
        Long companyId = 1232141425L;
        courseBaseInfoService.deleteCourseBase(id, companyId);
    }
}
